package com.xdjd.mobile.business.service;

import com.gitee.easyopen.exception.ApiException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xdjd.mobile.business.dao.mapper.activity.ActivityClockSuperviseRecordPoDao;
import com.xdjd.mobile.business.dao.mapper.activity.ActivityUserClockPoDao;
import com.xdjd.mobile.business.dao.mapper.member.MemberUserFocusPoDao;
import com.xdjd.mobile.business.dao.mapper.member.MemberUserPoDao;
import com.xdjd.mobile.business.entity.model.activity.ActivityClockSuperviseRecordPo;
import com.xdjd.mobile.business.entity.model.activity.ActivityUserClockPo;
import com.xdjd.mobile.business.entity.model.member.MemberUserFocusPo;
import com.xdjd.mobile.business.entity.model.member.MemberUserPo;
import com.xdjd.mobile.business.entity.param.BasePageParam;
import com.xdjd.mobile.business.entity.result.BasePageResult;
import com.xdjd.mobile.business.entity.vo.FriendListVO;
import com.xdjd.mobile.business.entity.vo.FriendLockInfoVO;
import com.xdjd.mobile.business.entity.vo.FriendSearchVO;
import net.oschina.durcframework.easymybatis.query.Query;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FriendService {

    @Autowired
    private MemberUserFocusPoDao memberUserFocusPoDao;
    @Autowired
    private MemberUserPoDao memberUserPoDao;
    @Autowired
    private ActivityUserClockPoDao activityUserClockPoDao ;
    @Autowired
    private ActivityClockSuperviseRecordPoDao activityClockSuperviseRecordPoDao ;


    public FriendSearchVO search(String searchInfo) {

        //默认搜索信息是手机号
        MemberUserPo memberUserPo = memberUserPoDao.findByAccountOrInviteCode(searchInfo);
        if (memberUserPo != null) {
            Query query = new Query().eq("member_id", memberUserPo.getId()).eq("status", 1);
            ActivityUserClockPo activityUserClockPo = activityUserClockPoDao.getByExpression(query);
            FriendSearchVO friendSearchVO = new FriendSearchVO();
            friendSearchVO.setNickName(memberUserPo.getNickName());
            friendSearchVO.setImage(memberUserPo.getImage());
            friendSearchVO.setMemberId(memberUserPo.getId());
            if (activityUserClockPo != null) {
                friendSearchVO.setTotalDay(activityUserClockPo.getDayNumber());
                friendSearchVO.setTotalStep(activityUserClockPo.getDayStepNumber());
                friendSearchVO.setBonusPool(activityUserClockPo.getBonusPool());
            }
            return friendSearchVO;
        } else {
            throw new ApiException("无此用户", "-1");
        }
    }

    /**
     * @param curUserId 当前用户id
     * @param memberId  关注用户id
     */
    public void focus(Long curUserId, Long memberId) {
        Query query = new Query().eq("member_id", curUserId).eq("focus_member_id", memberId);
        MemberUserFocusPo memberUserFocusPo = memberUserFocusPoDao.getByExpression(query);
        if (memberUserFocusPo != null) {
            throw new ApiException("您已经关注该用户");
        }
        memberUserFocusPo = new MemberUserFocusPo();
        memberUserFocusPo.setCreateTime(new Date());
        memberUserFocusPo.setMemberId(curUserId);
        memberUserFocusPo.setFocusMemberId(memberId);
        memberUserFocusPoDao.saveIgnoreNull(memberUserFocusPo);
    }


    public void friendNotice(Long curUserId, Long memberId) {

    }

    public BasePageResult<FriendListVO> friendList(Long id, BasePageParam param) {
        PageHelper.startPage(param.getPageIndex(), param.getPageSize());
        List<FriendListVO> friendListVOS = memberUserFocusPoDao.friendList(id);
        PageInfo pageInfo = new PageInfo(friendListVOS);
        if (CollectionUtils.isNotEmpty(friendListVOS)) {
            Long status;
            for (FriendListVO friendListVO : friendListVOS) {
                status = friendListVO.getStatus();
                if (status != null) {
                    friendListVO.setStatus(1L);
                } else {
                    friendListVO.setStatus(0L);
                }

            }
        }
        return new BasePageResult<>(pageInfo.getTotal(), friendListVOS);
    }

    public FriendLockInfoVO friendLockInfo(Long curUserId ,Long memberId) {
        FriendLockInfoVO friendLockInfoVO =  memberUserFocusPoDao.friendLockInfo(curUserId,memberId);
        if (friendLockInfoVO != null){
            Long status = friendLockInfoVO.getStatus() ;
            if (status == null){
                friendLockInfoVO.setStatus(0L);
            }else{
                friendLockInfoVO.setStatus(1L);
            }
        }
        return friendLockInfoVO ;
    }

    public void friendSupervise(Long id, String clockNo) {
        Query query = new Query().eq("member_id",id).eq("clock_no",clockNo);
        Long count = activityClockSuperviseRecordPoDao.countTotal(query);
        if (count > 0){
            throw new ApiException("您已经监督过该打卡记录","-1");
        }
        ActivityClockSuperviseRecordPo activityClockSuperviseRecordPo = new ActivityClockSuperviseRecordPo();
        activityClockSuperviseRecordPo.setMemberId(id);
        activityClockSuperviseRecordPo.setClockNo(clockNo);
        activityClockSuperviseRecordPo.setAdditionalBonus(0L);
        activityClockSuperviseRecordPo.setCreateTime(new Date());
        activityClockSuperviseRecordPoDao.saveIgnoreNull(activityClockSuperviseRecordPo);
    }
}
